package com.atguigu.gmall.common.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @ClassName Json
 * @Description TODO
 * @Author king
 * @Date 2022/11/14 21:14
 * @Version 1.0
 */
public class Json {

    static final ObjectMapper om = new ObjectMapper();

    public static String toJsonStr(Object obj){
        String s = null;
        try {
            s = om.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return s;
    }

    public static <T> T Obj(String userJson, Class<T> userInfoClass) {
        T t = null;
        try {
            t = om.readValue(userJson, userInfoClass);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return t;
    }
}
